package com.mediacloud.app.newsmodule.activity.military_encyclopedia

import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import androidx.recyclerview.widget.RecyclerView
import com.mediacloud.app.model.activity.BaseBackActivity
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.utils.ViewUtils
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.adaptor.component.WikiRankList
import com.mediacloud.app.newsmodule.fragment.baoliao.api.SpiderCmsApi
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils
import com.mediacloud.app.quanzi.model.BaseDataListBean
import com.mediacloud.app.view.XSmartRefreshLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import com.utils.ClickUtils
import org.jetbrains.anko.backgroundColor
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class WikiListActivity : BaseBackActivity(), OnLoadMoreListener, OnRefreshListener {
    var page: Int = 1
    var perPage: Int = 20
    var rankType: Int = 0
    var wikiList: RecyclerView? = null
    var wikiFresh: XSmartRefreshLayout? = null
    var wikiListAdapter: WikiListAdapter? = null

    override fun getLayoutResID(): Int {
        return R.layout.activity_wikilist
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    private fun initView() {
        rankType = intent.getIntExtra("wikiType", 0)
        var title = intent.getStringExtra("wikiTitle")
        if (!TextUtils.isEmpty(title)) {
            setTitle(title)
        } else {
            setTitle("百科榜单")
        }
        mTitileBar.backgroundColor = Color.WHITE
        loadingView = findViewById(R.id.mLoadingView)
        wikiList = findViewById(R.id.wikiList)
        wikiFresh = findViewById(R.id.wikiFresh)
        wikiFresh?.setOnLoadMoreListener(this)
        wikiFresh?.setOnRefreshListener(this)
        wikiList?.layoutManager = ViewUtils.generateRecyclerLinearLayoutManager(this, true)
        wikiListAdapter = WikiListAdapter()
        wikiListAdapter?.rankType = rankType
        wikiList?.adapter = wikiListAdapter
        wikiListAdapter?.setOnItemClickListener { adapter, view, position ->
            ClickUtils.delayClickable(view)
            val item = adapter.getItem(position) as WikiRankList
            var articleItem = ArticleItem()
            articleItem.id = item.id
            articleItem.type = item.type
            NewsItemClickUtils.OpenItemNewsHandle(view.context, articleItem.type, articleItem, null)
        }
        getBkData()
    }

    private fun getBkData() {
        SpiderCmsApi.getSpiderCmsApi(this)
            .getWikiRanking(page, perPage, rankType)
            .enqueue(object : Callback<BaseDataListBean<WikiRankList>> {
                override fun onResponse(
                    call: Call<BaseDataListBean<WikiRankList>>,
                    response: Response<BaseDataListBean<WikiRankList>>
                ) {
                    if (isFinish || isDestroyed) return
                    closeStateView()
                    if (response.body()?.isSuccess == true) {
                        val data = response.body()?.data
                        data?.let {
                            wikiFresh?.finishRefresh()
                            if (it.isHasNextPage) {
                                wikiFresh?.finishLoadMore()
                            } else {
                                wikiFresh?.finishLoadMoreWithNoMoreData()
                            }
                            if (page == 1) {
                                if (it.pageRecords.isNotEmpty()) {
                                    wikiListAdapter?.setNewData(data.pageRecords)
                                } else {
                                    showStateView(TYPE_NO_CONTENT, false)
                                }
                            } else {
                                wikiListAdapter?.addData(data.pageRecords)
                            }
                        }

                    } else {
                        showStateView(TYPE_NO_CONTENT, false)

                    }
                }

                override fun onFailure(call: Call<BaseDataListBean<WikiRankList>>, t: Throwable) {
                    showStateView(TYPE_NET_NOT_GIVE_FORCE, false)
                }

            })
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        page++
        getBkData()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        page = 1
        getBkData()
    }


    override fun getStatusBarColor(): Int {
        return Color.WHITE
    }
}